package com.yz.jdd.module.settings;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.module.settings.model.PayQrCode;


import java.util.List;
import java.util.Map;

public class SelectCodeAdapter extends RecyclerView.Adapter<SelectCodeAdapter.MyViewHolder> {

    private Context context;


    /**
     * 先造一组模拟数据
     */
    private List<PayQrCode> listData;
    SelectCodeActivity activity;


    public SelectCodeAdapter(Context context,List<PayQrCode> listData) {
        this.context = context;
        this.listData=listData;
        activity= (SelectCodeActivity) context;

    }



    public class MyViewHolder extends RecyclerView.ViewHolder {
        CheckBox select_code_check;
        TextView select_code_name,select_code_id,select_code_cashier;

        public MyViewHolder(View itemView) {
            super(itemView);
            select_code_check = itemView.findViewById(R.id.select_code_check);
            select_code_name = itemView.findViewById(R.id.select_code_name);
            select_code_id= itemView.findViewById(R.id.select_code_id);
            select_code_cashier= itemView.findViewById(R.id.select_code_cashier);
        }
    }

    @NonNull
    @Override
    public SelectCodeAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.item_code_select, parent, false);
        MyViewHolder holder = new MyViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final SelectCodeAdapter.MyViewHolder holder, final int position) {
        final PayQrCode payQrCode=listData.get(position);
        String textStr="";
        if(!TextUtils.isEmpty(payQrCode.getQrcodeName())){
            textStr=payQrCode.getQrcodeName();
        }

        if("1".equals(payQrCode.getQrcodeType())){
            textStr = "<font color=\"#FF211A\">" + "[基础码] " + "</font>"+textStr;
        }else if("2".equals(payQrCode.getQrcodeType())){
           textStr = "<font color=\"#459DFF\">" + "[小程序码] " + "</font>"+textStr;
        }else if("3".equals(payQrCode.getQrcodeType())){
            textStr = "<font color=\"#FF211A\">" + "[基础码] " + "</font>"+textStr;
        }


        holder.select_code_name.setText(Html.fromHtml(textStr));
        holder.select_code_id.setText(TextUtils.isEmpty(payQrCode.getQrcodeId())?"码ID:--":"码ID:"+payQrCode.getQrcodeId());
        holder.select_code_cashier.setText(TextUtils.isEmpty(payQrCode.getCashierName())?"收银员:未绑定":"收银员:"+payQrCode.getCashierName());
        //清掉监听器
        holder.select_code_check.setOnCheckedChangeListener(null);
        //设置选中状态
        holder.select_code_check.setChecked(payQrCode.isSelected());
        //再设置监听器
        holder.select_code_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //check状态一旦改变，保存的check值也要发生相应的变化
//                checkStatus.put(position, isChecked);
                payQrCode.setSelected(isChecked);
                activity.setSelectSize(listData);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.select_code_check.performClick();
            }
        });
    }

    @Override
    public int getItemCount() {
        if (listData == null){
            return 0;
        }
        return listData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }
}